const HashTable = require("../template")

function testHashTableCollisionHandling() {
  const ht = new HashTable(4) // 使用小容量强制产生冲突

  console.log(
    "1. 哈希冲突基本测试:",
    (() => {
      // 使用相同的哈希值来测试冲突处理
      ht.set('key1', 'value1')
      ht.set('key2', 'value2')
      ht.set('key3', 'value3')
      ht.set('key4', 'value4')
      
      return ht.get('key1') === 'value1' &&
             ht.get('key2') === 'value2' &&
             ht.get('key3') === 'value3' &&
             ht.get('key4') === 'value4' &&
             ht.size() === 4
    })()
  )

  console.log(
    "2. 冲突键值对更新测试:",
    (() => {
      ht.set('key1', 'updated_value1')
      ht.set('key2', 'updated_value2')
      
      return ht.get('key1') === 'updated_value1' &&
             ht.get('key2') === 'updated_value2' &&
             ht.size() === 4 // 大小不应该改变
    })()
  )

  console.log(
    "3. 冲突键值对删除测试:",
    (() => {
      ht.delete('key1')
      ht.delete('key3')
      
      return ht.get('key1') === undefined &&
             ht.get('key3') === undefined &&
             ht.get('key2') === 'updated_value2' &&
             ht.get('key4') === 'value4' &&
             ht.size() === 2
    })()
  )

  console.log(
    "4. 冲突键存在性检查测试:",
    (() => {
      return ht.has('key1') === false &&
             ht.has('key2') === true &&
             ht.has('key3') === false &&
             ht.has('key4') === true
    })()
  )

  console.log(
    "5. 冲突后键值对遍历测试:",
    (() => {
      const keys = ht.keys()
      const values = ht.values()
      const entries = ht.entries()
      
      return keys.length === 2 &&
             values.length === 2 &&
             entries.length === 2 &&
             keys.includes('key2') &&
             keys.includes('key4') &&
             values.includes('updated_value2') &&
             values.includes('value4')
    })()
  )

  console.log(
    "6. 大量冲突测试:",
    (() => {
      const largeHt = new HashTable(2) // 非常小的容量
      
      // 插入大量数据强制冲突
      for (let i = 0; i < 10; i++) {
        largeHt.set(`key${i}`, `value${i}`)
      }
      
      // 验证所有数据都能正确获取
      let allCorrect = true
      for (let i = 0; i < 10; i++) {
        if (largeHt.get(`key${i}`) !== `value${i}`) {
          allCorrect = false
          break
        }
      }
      
      return allCorrect && largeHt.size() === 10
    })()
  )

  console.log(
    "7. 冲突链中查找测试:",
    (() => {
      const chainHt = new HashTable(1) // 单桶，所有键都会冲突
      
      chainHt.set('a', 1)
      chainHt.set('b', 2)
      chainHt.set('c', 3)
      chainHt.set('d', 4)
      
      return chainHt.get('a') === 1 &&
             chainHt.get('b') === 2 &&
             chainHt.get('c') === 3 &&
             chainHt.get('d') === 4 &&
             chainHt.get('e') === undefined
    })()
  )

  console.log(
    "8. 冲突链中删除测试:",
    (() => {
      const chainHt = new HashTable(1)
      
      chainHt.set('a', 1)
      chainHt.set('b', 2)
      chainHt.set('c', 3)
      chainHt.set('d', 4)
      
      chainHt.delete('b') // 删除中间的键
      chainHt.delete('a') // 删除第一个键
      chainHt.delete('d') // 删除最后一个键
      
      return chainHt.get('a') === undefined &&
             chainHt.get('b') === undefined &&
             chainHt.get('c') === 3 &&
             chainHt.get('d') === undefined &&
             chainHt.size() === 1
    })()
  )

  console.log(
    "9. this上下文冲突处理测试:",
    (() => {
      const collisionObj = {
        multiplier: 2,
        processCollision: function(entries) {
          const ht = new HashTable(2) // 小容量强制冲突
          entries.forEach(([key, value]) => ht.set(key, value))
          return ht.size() * this.multiplier
        }
      }
      return collisionObj.processCollision([['a', 1], ['b', 2], ['c', 3]]) === 6 // 3*2=6
    })()
  )

  console.log(
    "10. 复杂对象this绑定冲突测试:",
    (() => {
      const collisionConfigObj = {
        config: { threshold: 2 },
        processWithConfig: function(entries) {
          const ht = new HashTable(1) // 单桶强制冲突
          entries.forEach(([key, value]) => ht.set(key, value))
          return ht.size() >= this.config.threshold
        }
      }
      return collisionConfigObj.processWithConfig([['a', 1], ['b', 2], ['c', 3]]) === true
    })()
  )

  console.log(
    "11. 冲突后负载因子测试:",
    (() => {
      const loadFactorHt = new HashTable(4)
      
      loadFactorHt.set('a', 1)
      loadFactorHt.set('b', 2)
      loadFactorHt.set('c', 3)
      loadFactorHt.set('d', 4)
      
      const loadFactor = loadFactorHt.getLoadFactor()
      return loadFactor === 1.0 && loadFactorHt.getCapacity() === 4
    })()
  )

  console.log(
    "12. 冲突键值对一致性测试:",
    (() => {
      const consistencyHt = new HashTable(2)
      
      consistencyHt.set('key1', 'value1')
      consistencyHt.set('key2', 'value2')
      consistencyHt.set('key3', 'value3')
      
      const size1 = consistencyHt.size()
      consistencyHt.delete('key2')
      const size2 = consistencyHt.size()
      
      return size1 === 3 && 
             size2 === 2 &&
             consistencyHt.get('key1') === 'value1' &&
             consistencyHt.get('key3') === 'value3' &&
             consistencyHt.get('key2') === undefined
    })()
  )
}

testHashTableCollisionHandling()
